package mk.timska_rabota_android.location;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
	
public class LocationModule {
	public static Location Network = null;
	public static Location GPS = null;
	private static Location LastKnown = null;
	private static LocationListener locationListenerGPS;
	private static LocationListener locationListenerNetwork;
	private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
	private static LocationManager locationManager;
	private static StringBuilder status = new StringBuilder();
	
	public static Location getLastKnownLocation(Context con, Activity act){
		if(locationManager == null && locationManager == null){
			status.append("Location Manager not called.");
			Log.d("LOCATION", status.toString());
			return null;
		}
		Location tempNetwork = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
		Location tempGPS = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
		if (tempNetwork == null && tempGPS == null) {
			status.append("Location Request Failed.");
			Log.d("LOCATION", status.toString());
			return null;
		} else {
			if(tempGPS!= null)LastKnown = tempGPS;
			else LastKnown = tempNetwork;
			status = new StringBuilder();
			status.append(LastKnown.getProvider());
			status.append(" \tLatitude = ");
			status.append(LastKnown.getLatitude());
			status.append(" \tLongitude = ");
			status.append(LastKnown.getLongitude());
			if(LastKnown.hasAccuracy()){
				status.append(" \tAccuracy = ");
				status.append(LastKnown.getAccuracy());
			} else {
				status.append(" \tAccuracy not available.");
			}
			Log.d("LOCATION", status.toString());
			return LastKnown;
		}
	}
	
	public static void UpdateLocation(final Context con,final Activity act){
		if(locationManager==null)locationManager = (LocationManager) act.getSystemService(Context.LOCATION_SERVICE);
		if (locationListenerGPS == null) {
			locationListenerGPS = new LocationListener() {
				public void onLocationChanged(Location location) {
					status = new StringBuilder();
					GPS = location;
					status.append("GPS_PROVIDER: Latitude = ");
					status.append(GPS.getLatitude());
					status.append(" \tLongitude = ");
					status.append(GPS.getLongitude());
					if(GPS.hasAccuracy()){
						status.append(" \tAccuracy = ");
						status.append(GPS.getAccuracy());
					} else {
						status.append(" \tAccuracy not available.");
					}
					Log.i("GPS_UPDATE", status.toString());
					//Toast t = Toast.makeText(con, status.toString(), Toast.LENGTH_LONG);
					//t.show();
					
					if(GPS.hasAccuracy()){
						if(GPS.getAccuracy()<100){
							locationManager.removeUpdates(locationListenerGPS);
						}
					}
				}

				public void onStatusChanged(String provider, int status,
						Bundle extras) {
				}

				public void onProviderEnabled(String provider) {
				}

				public void onProviderDisabled(String provider) {
				}

			};
		}
		if(locationManager==null)locationManager = (LocationManager) act.getSystemService(Context.LOCATION_SERVICE);
		if (locationListenerNetwork == null) {
			locationListenerNetwork = new LocationListener() {
				public void onLocationChanged(Location location) {
					Network = location;
					status = new StringBuilder();
					status.append("NETWORK_PROVIDER: Latitude = ");
					status.append(Network.getLatitude());
					status.append(" \tLongitude = ");
					status.append(Network.getLongitude());
					if(Network.hasAccuracy()){
						status.append(" \tAccuracy = ");
						status.append(Network.getAccuracy());
					} else {
						status.append(" \tAccuracy not available.");
					}
					Log.i("GPS_UPDATE", status.toString());
					//Toast t = Toast.makeText(con, status.toString(), Toast.LENGTH_LONG);
					//t.show();
					
					if(Network.hasAccuracy()){
						if(Network.getAccuracy()<1000){
							locationManager.removeUpdates(locationListenerNetwork);
						}
					}
				}

				public void onStatusChanged(String provider, int status,
						Bundle extras) {
				}

				public void onProviderEnabled(String provider) {
				}

				public void onProviderDisabled(String provider) {
				}

			};
			
			
		}
		if(GPS == null){
			locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListenerGPS);
		}
		else{
			locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListenerGPS);
		}
		
		if(Network == null){
			locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,50,locationListenerNetwork);
		}
		else{
			locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,2,50,locationListenerNetwork);
		}
		
	}
}
